package cn.jarlen.jwt.tools.antivirus;

import android.content.Context;
import android.util.Log;

import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;

import cn.jarlen.jwt.tools.menu.IMenuProcess;

/**
 * @author jarlen
 * Create by 2024/7/3 11:13
 */
public class AndroidCVEDataClean implements IMenuProcess {
    @Override
    public void onExecute(Context context) {
        Log.w("AndroidCVEDataClean", "清洗AndroidCVE 开始");
        // List<CVELib> antivirusList = AntivirusDao.getInstance(context).queryAndroidCVE();
        // if (antivirusList == null || antivirusList.isEmpty()) {
        //     return;
        // }
        // for (CVELib antivirus : antivirusList) {
        //     antivirus.setSystem("Android");
        //     String level = antivirus.getCveLevel();
        //     if ("-4".equals(level)) {
        //         antivirus.setCveLevel("高");
        //     } else if ("-3".equals(level)) {
        //         antivirus.setCveLevel("严重");
        //     } else if ("-2".equals(level))
        //         antivirus.setCveLevel("中");
        //     else if ("-1".equals(level)) {
        //         antivirus.setCveLevel("低");
        //     }
        //
        //     String version = antivirus.getAffectedVersion();
        //     version = version.replaceAll("、", ",");
        //     antivirus.setAffectedVersion(version);
        //
        //     String patchVer = antivirus.getPatchVer();
        //     patchVer = patchVer.trim();
        //     if (patchVer.length() < 11) {
        //         int yearIndex = patchVer.indexOf("年");
        //         int monthIndex = patchVer.indexOf("月");
        //         int dayIndex = patchVer.indexOf("日");
        //         if (monthIndex - yearIndex < 3) {
        //             patchVer = patchVer.replace("年", "年0");
        //         }
        //         if (dayIndex - monthIndex < 3) {
        //             patchVer = patchVer.replace("月", "月0");
        //         }
        //     }
        //
        //     antivirus.setPatchVer(CVEHelper.toFormatDate(patchVer, "yyyy年MM月dd日", "yyyy-MM-dd"));
        // }

        String androidCVEJson = getFromAssets(context, "AndroidCVE.json");
        List<CVELib> antivirusList = JSON.parseArray(androidCVEJson, CVELib.class);
        Log.w("AndroidCVEDataClean", "清洗AndroidCVE 完成:" + JSON.toJSONString(antivirusList));


        AntivirusDao.getInstance(context).insertAntivirus(antivirusList);
        Log.w("AndroidCVEDataClean", "漏洞库入库 完成");
    }

    public String getFromAssets(Context context, String fileName) {
        try {
            InputStreamReader inputReader = new InputStreamReader(context.getResources().getAssets().open(fileName));
            BufferedReader bufReader = new BufferedReader(inputReader);
            String line = "";
            String result = "";
            while ((line = bufReader.readLine()) != null)
                result += line;
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


}
